//
//  LoginViewController.m
//  Politics
//
//  Created by Hai Nguyen on 7/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistryViewController.h"
#import "MainViewController.h"
#import "AppDelegate_iPhone.h"
#import "FBPoster.h"
#import "FBUtilities.h"

@implementation LoginViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.view.alpha = 0.0;
}

- (void)viewDidAppear:(BOOL)animated {
	NSLog(@"view did appear");
	[self animationAppearance];
}
- (void)login:(NSString*)userId password:(NSString*)userPassword
{
	if ([userId length]==0)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
														message:@"Email address should not empty" 
													   delegate:nil 
											  cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	if ([userPassword length] == 0)
	{
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
														message:@"Password address should not empty" 
													   delegate:nil 
											  cancelButtonTitle:@"Close" otherButtonTitles:nil];
		[alert show];
		[alert release];
		return;
	}
	LoginParser *loginParser = [[LoginParser alloc] init];
	[loginParser loginWithUserId:userId password:userPassword withDelegate:self];
	[loginParser release];
}
- (void)didLogin:(int)result;
{
	//result = 1; // for test
	switch (result)
	{
		case 0:
		{
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" 
															message:@"Email or Password invalid, try again" 
														   delegate:nil 
												  cancelButtonTitle:@"Close" otherButtonTitles:nil];
			[alert show];
			[alert release];			
		}
			break;			
		case 1:
			//bgView.alpha = 0.0;
		{	
			mainiPhoneAppDelegate.username = username.text;
			[password setText:@""];
			MainViewController *controller = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
			[self.navigationController pushViewController:controller animated:YES];
			[controller release];			
		}
			break;
			
		default:
			break;
	}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)animationAppearance {
	//animations
	self.view.transform = CGAffineTransformIdentity;
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.01];
	self.view.alpha = 0.0;
	self.view.transform = CGAffineTransformMakeScale(0.05, 0.05);	
	[UIView commitAnimations];	
	self.view.alpha = 0.0;
	[UIView beginAnimations:nil context:nil];   
	[UIView setAnimationDuration:0.25];   
	self.view.alpha = 1.0;
	//CGAffineTransform transform = CGAffineTransformMakeScale(1, 1);  
	self.view.transform = CGAffineTransformMakeScale(1.0,1.0);
	[UIView commitAnimations];
	
}

- (IBAction) buttonPressed:(id) sender
{
	UIButton *button = (UIButton*) sender;
	switch (button.tag) {
		case 1:
		{
			[Audio playButtonClicked];
			[self login:username.text password:password.text];
		}
			break;
		case 2:
		{	
			[Audio playButtonClicked];
			// register button
			if(!registerController){
				registerController = [[RegistryViewController alloc] initWithNibName:@"RegistryViewController" bundle:nil];
				registerController.controller = self;
			}
			registerController.view.frame = CGRectMake(0,0,320,480);
			registerController.view.alpha = 0.0;
			[self.view addSubview:registerController.view];
			[UIView beginAnimations:nil context:nil];
			[UIView setAnimationDuration:0.5];
			registerController.view.alpha = 1.0;
			bgView.alpha = 0.0;
			[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
			[UIView commitAnimations];
		}
			break;
			case 3:
			[username resignFirstResponder];
			[password resignFirstResponder];
				break;
			case 4:
		{
			FBSession *fbSession = [FBSession session];
			if (fbSession == nil)
			{
				[mainiPhoneAppDelegate.FButilities facebookLogin:self];
			}
			else if(fbSession.isConnected)
			{
				[FBPoster PostToWall:@"Today, I have played the Politics Game. It's so fun!"];
			}
			else
			{
				[mainiPhoneAppDelegate.FButilities facebookLogin:self];
			}
		}
			break;

		default:
			break;
	}
}

#pragma mark -
#pragma mark TextField Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if ([textField isEqual:username])
	{
		[password becomeFirstResponder];
	}
	else
	{
		[textField resignFirstResponder];
	}
	return YES;
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark RegisterDelegate
- (void)finishRegistryWithStatus:(int)status {
	switch (status) {
		case RegisterCancel:{
			// remove register screen
			[UIView beginAnimations:nil context:nil];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.5];
			bgView.alpha = 1.0;
			registerController.view.alpha = 0.0;
			[UIView setAnimationDidStopSelector:@selector(finishRemoveRegister)];
			[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:NO];
			[UIView commitAnimations];
		}
		break;
		case RegisterSusccess:
		{
			// move to main menu screen.
			MainViewController *controller = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
			[self.navigationController pushViewController:controller animated:YES];
			[controller release];
			
		}
		break;

		default:
			break;
	}
	
}

- (void)finishRemoveRegister {
	[registerController.view removeFromSuperview];
	//[registerController release];
}

@end
